﻿using System;
using System.Text.RegularExpressions;

namespace EGConfig
{
    public enum EConfigType
    {
        None,
        Int,
        Long,
        Float,
        Double,
        String,
        List,
        Dictionary,
    }

    public class ConfigType
    {
        public string stringType;
        public bool IsBaseValue;
        public EConfigType MainType;
        public ConfigType SubType1;
        public ConfigType SubType2;

        public ConfigType(string stringType)
        {
            this.stringType = stringType;
            Init();
        }

        private void Init()
        {
            if (stringType == "int")
            {
                IsBaseValue = true;
                MainType = EConfigType.Int;
            }
            else if (stringType == "long")
            {
                IsBaseValue = true;
                MainType = EConfigType.Long;
            }
            else if (stringType == "float")
            {
                IsBaseValue = true;
                MainType = EConfigType.Float;
            }
            else if (stringType == "double")
            {
                IsBaseValue = true;
                MainType = EConfigType.Double;
            }
            else if (stringType == "string")
            {
                IsBaseValue = true;
                MainType = EConfigType.String;
            }
            else if (stringType.StartsWith("List"))
            {
                MainType = EConfigType.List;
                string stringSubType1 = Regex.Match(stringType, "List<(.+)>").Groups[1].Value;
                SubType1 = new ConfigType(stringSubType1);
            }
            else if (stringType.StartsWith("Dictionary"))
            {
                MainType = EConfigType.Dictionary;
                Match match = Regex.Match(stringType, "Dictionary<(.+),(.+)>");
                string stringSubType1 = match.Groups[1].Value;
                string stringSubType2 = match.Groups[2].Value;
                SubType1 = new ConfigType(stringSubType1);
                SubType2 = new ConfigType(stringSubType2);
            }
        }


        public string ToStringType()
        {
            if (IsBaseType(MainType))
            {
                return BaseTypeToString(MainType);
            }

            if (MainType == EConfigType.List)
            {
                string subTypeName = SubType1.ToStringType();
                return $"List<{subTypeName}>";
            }

            if (MainType == EConfigType.Dictionary)
            {
                string subTypeName1 = SubType1.ToStringType();
                string subTypeName2 = SubType2.ToStringType();
                return $"Dictionary<{subTypeName1},{subTypeName2}>";
            }

            throw new Exception($"错误的类型:{MainType}");
        }

        private bool IsBaseType(EConfigType eConfigType)
        {
            switch (eConfigType)
            {
                case EConfigType.Int:
                case EConfigType.Long:
                case EConfigType.Float:
                case EConfigType.Double:
                case EConfigType.String:
                {
                    return true;
                }
                default:
                    return false;
            }
        }

        private string BaseTypeToString(EConfigType eConfigType)
        {
            switch (eConfigType)
            {
                case EConfigType.Int:
                {
                    return "int";
                }
                case EConfigType.Long:
                {
                    return "long";
                }
                case EConfigType.Float:
                {
                    return "float";
                }
                case EConfigType.Double:
                {
                    return "double";
                }
                case EConfigType.String:
                {
                    return "string";
                }
                default:
                    throw new Exception($"{eConfigType}:未知或不是基础类型");
            }
        }
    }
}